package com.laikan.reader.bookreader.engines;

import android.app.Activity;
import android.content.Context;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.laikan.library.httpvolley.engine.HttpApiRequestEngine;
import com.laikan.library.httpvolley.engine.Requester;
import com.laikan.library.httpvolley.helper.RequestListener;
import com.laikan.library.httpvolley.helper.VolleyErrorHandler;
import com.laikan.library.httpvolley.response.HttpResponse;
import com.laikan.library.managers.ToastMgr;
import com.laikan.library.utils.SPUtil;
import com.laikan.reader.R;
import com.laikan.reader.comment.APIProtocol;
import com.laikan.reader.core.bean.BaseDataBean;
import com.laikan.reader.core.bean.ChapterDetailBean;
import com.socks.library.KLog;

import java.lang.reflect.Type;
import java.util.HashMap;

public class EngineOP {

    private BuyChapterListener buyChapterListener;

    public EngineOP(BuyChapterListener buyChapterListener) {
        this.buyChapterListener = buyChapterListener;
    }

    public interface BuyChapterListener {

        public void buySuceeess(String chid);
    }

    /**
     * 购买章节
     */
    public void chapterBuy(final Context context, final String bookid, final String chid, final int typeRef, final int goInType, final boolean isAuto,boolean isAutoBuy) {
        String autoBuy = isAutoBuy ? "true" : "false";
        final HashMap<String, String> map = new HashMap<String, String>();
        map.put("sd", SPUtil.getString("token", ""));
        map.put("useTicket", "true");
        map.put("auto", autoBuy);

        RequestListener requestListener = new RequestListener() {
            @Override
            public void onPreExecute() { KLog.e("---------volley onPreExecute!!!"); }

            @Override
            public void onSuccess(HttpResponse httpResponse) {
                BaseDataBean<ChapterDetailBean> result;
                Gson gson = new Gson();
                Type type = new TypeToken<BaseDataBean<ChapterDetailBean>>() {
                }.getType();
                SPUtil.putBoolean("login_laikan_setting", true);
                try {
                    result = gson.fromJson(httpResponse.responseContent, type);
                }catch (Exception e) {
                    ToastMgr.showShortToast(R.string.laikan_data_load_failed);
                    return;
                }
                if (result != null) {
                    if (null == result.getData() || !"1".equals(result.getResult())) {

                        ToastMgr.showShortToast(result.getError_msg());
                        KLog.e("70 result.getError_msg() = " + result.getError_msg());
                    }else {
                        if (!isAuto) {
                            if (typeRef != -1) {
                                SPUtil.putBoolean("IsRefer", true);
                                SPUtil.putInt("typeRef", typeRef);
                            }
                            SPUtil.putInt(bookid + chid + "buyback", goInType);
                            ((Activity)context).finish();
                        }

                        if (buyChapterListener != null) {
                            buyChapterListener.buySuceeess(chid);
                        }else {
                            ToastMgr.showShortToast("购买成功");
                        }

                    }
                }else {
                    ToastMgr.showShortToast(R.string.laikan_data_load_failed);
                }
            }

            @Override
            public void onError(VolleyErrorHandler errorHandler) {
                KLog.e("网络不给力，请重试。39");
            }

            @Override
            public void onFinish() {

            }
        };

        Requester requester = new Requester();
        requester.setUrl(APIProtocol.getRootURL() + "/buy/chapter/" + chid + "?");
        requester.setMapBody(map);
        HttpApiRequestEngine.getInstance().httpPostRequest(requester, requestListener);

//        HttpApiRequestEngine.getInstance().httpPostRequest(APIProtocol.getRootURL() + "/buy/chapter/" + chid + "?", map, null, null, requestListener);
    }

}
